#include <stdio.h>
#include <stdbool.h>

bool isValid(char* s) {
    char stack[10001] = {0};
    int idx = 0;

#define isNotEmpty() ( idx > 0 )
#define push(x) do {stack[idx++] = (x);} while(0)
#define pop(x) do {if (isNotEmpty()){ (x) = stack[--idx];}} while(0) 

    char* p = s;
    char left;
    while (*p) {
        if (*p == '(' || *p == '{' || *p == '[') {
            push(*p);
        } else {
            if (!isNotEmpty())
                return false;
            pop(left);
            switch (*p) {
            case ')': if (left != '(') return false; break;
            case '}': if (left != '{') return false; break;
            case ']': if (left != '[') return false; break;
            default:
                return false;
            }
        }
        p++;
    }
    return isNotEmpty() ? false : true;
}

int main(void)
{
    printf("#20. 有效的括号\n\n");
    printf("用c的数组快速构建一个栈\n\n");
    char *s = "{[]}";
    printf("ans: %s\n", isValid(s) ? "true": "false");
    return 0;
}